#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define maxn 1001
#define cst 131
#define inf 0x7f7f7f7f
#define Rep(i,a,b) for(int i=(a);i<=(b);++i)
#define Per(i,a,b) for(int i=(a);i>=(b);--i)
int n;
map<string,bool>vis;
map<string,int>num;
struct node {
	string type;
	string addr;
	bool err;
	string ans;
	int pans;
} a[maxn];
inline void Connect(int i) {
	if(a[i].err)a[i].ans="ERR";
	else if(a[i].type=="Server") {
		if(!vis[a[i].addr])
			a[i].ans="OK",num[a[i].addr]=i,vis[a[i].addr]=1;
		else a[i].ans="FAIL";
	} else {
		if(vis[a[i].addr])
			a[i].ans="a",a[i].pans=num[a[i].addr];
		else a[i].ans="FAIL";
	}
}
inline void Check(int i) {
	int tot=0,s=0;
	int len=a[i].addr.length();
	Rep(j,0,len-1) {
		if(a[i].addr[j]=='.'&&tot<3) {
			++tot;
			s=0;
		} else if(a[i].addr[j]==':'&&tot==3) {
			++tot;
			s=0;
		} else if(a[i].addr[j]=='.'||a[i].addr[j]==':'){
			a[i].err=1;
			break;
		}
		if(a[i].addr[j]>='0'&&a[i].addr[j]<='9') {
			s=s*10+(a[i].addr[j]-48);
			if(tot<4&&s>255) {
				a[i].err=1;
				break;
			}
			if(tot==4&&s>65535) {
				a[i].err=1;
				break;
			}
		}
		if(tot>4) {
			a[i].err=1;
			break;
		}
	}
}
int main() {
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	scanf("%d",&n);
	Rep(i,1,n) {
		cin>>a[i].type>>a[i].addr;
		Check(i);
	}
	Rep(i,1,n) 
		Connect(i);
	Rep(i,1,n){
		if(a[i].ans!="a")cout<<a[i].ans<<endl;
		else cout<<a[i].pans<<endl;
	}
	return 0;
}
